package org.gao.test.transaction.Standalone.LocalDB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.spi.NamingManager;

public class LocalDatasource {
	
	private static LocalDatasource ds;
	private String url;
	private String userName;
	private String pwd;
	
	public LocalDatasource(String url, String userName, String pwd) {
		this.url = url;
		this.userName = userName;
		this.pwd = pwd;
	}
	
	public static void initialize(String url, String userName, String pwd) {
		try {
			
			ds = new LocalDatasource(url, userName, pwd);
			
			NamingManager.setInitialContextFactoryBuilder(new LocalContextFactory());
			Context ctx = new InitialContext();
			ctx.bind("jdbc/ds", ds);
			
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, userName, pwd);
    }
	
}
